﻿@using Microsoft.AspNetCore.Components.Forms
@using Shared.Extensions

@*@inherits PropertiesComponentBase*@
@*<div class="prop-group-box" style="min-height:80vh">*@
<div style="min-height:80vh">
    @*<div class="box-header">Defaults(默认通用属性)</div>*@
    <Collapse OnCollapseChanged="(aa)=>{   return Task.CompletedTask;}" IsAccordion="true">
        <CollapseItems>
            <CollapseItem Text="通用属性" IsCollapsed="false">
                <ValidateForm @ref="ValidateForm" Model="@ComponentData" OnValidSubmit="@OnValidComplexModel" OnFieldValueChanged="OnFieldValueChanged">
                    <EditorForm TModel="ComponentDto" ItemsPerRow="1" AutoGenerateAllItem="false">
                        <FieldItems>
                            <EditorItem @bind-Field="@context.Id">
                                <EditTemplate Context="value">
                                    <BootstrapInput IsSelectAllTextOnFocus="true" @bind-Value="value.Id" OnValueChanged="IdOnValueChanged"></BootstrapInput>
                                </EditTemplate>
                            </EditorItem>
                  
                            <EditorItem @bind-Field="@context.StoreName">
                                <EditTemplate Context="value">
                                    <Select TValue="string" Items="GetProList()" @bind-Value="value.StoreName" ShowSearch="true" />
                                </EditTemplate>
                            </EditorItem>
                            <EditorItem @bind-Field="@context.Parameters">
                                <EditTemplate Context="value">
                                    <div class="ppp2">
                                        <Collapse>
                                            <CollapseItems>
                                                <CollapseItem Text="过程参数">
                                                    <CheckboxList @bind-Value="@value.Parameters" Items="@Parameters" Color="Color.Danger"></CheckboxList>
                                                </CollapseItem>
                                            </CollapseItems>
                                        </Collapse>
                                    </div>
                                </EditTemplate>
                            </EditorItem>
                            <EditorItem @bind-Field="@context.Width">
                                <EditTemplate Context="value">
                                    <BootstrapInputNumber @bind-Value="value.Width" Max="12" Min="1" ShowButton="true" OnValueChanged="OnValueChangedForInt"></BootstrapInputNumber>
                                </EditTemplate>
                            </EditorItem>
                            <EditorItem @bind-Field="@context.Height">
                                <EditTemplate Context="value">
                                    <BootstrapInputNumber @bind-Value="value.Height" ShowButton="true" Step="10" OnValueChanged="OnValueChangedForInt"></BootstrapInputNumber>
                                </EditTemplate>
                            </EditorItem>
                            <EditorItem @bind-Field="@context.Color">
                                <EditTemplate Context="value">
                                    <Select @bind-Value="value.Color" OnValueChanged="OnValueChangedForColor"></Select>
                                </EditTemplate>
                            </EditorItem>
                            <EditorItem @bind-Field="@context.ShowLabel">
                                <EditTemplate Context="value">
                                    <Switch @bind-Value="value.ShowLabel" OnValueChanged="OnValueChangedForBool"></Switch>
                                </EditTemplate>
                            </EditorItem>
                            <EditorItem @bind-Field="@context.IsDisabled">
                                <EditTemplate Context="value">
                                    <Switch @bind-Value="value.IsDisabled" OnValueChanged="OnValueChangedForBool"></Switch>
                                </EditTemplate>
                            </EditorItem>
                            <EditorItem @bind-Field="@context.MutLanguage">
                                <EditTemplate Context="value">
                                    <Collapse>
                                        <CollapseItems>
                                            <CollapseItem Text="多语言设置">
                                                <BootstrapInput @bind-Value="value.MutLanguage.zh_CN" OnValueChanged="(v)=>{return FormDesigner.StateHasChangedAsync();}"></BootstrapInput>
                                                <BootstrapInput @bind-Value="value.MutLanguage.zh_TW" OnValueChanged="(v)=>{return FormDesigner.StateHasChangedAsync();}"></BootstrapInput>
                                                <BootstrapInput @bind-Value="value.MutLanguage.en_US" OnValueChanged="(v)=>{return FormDesigner.StateHasChangedAsync();}"></BootstrapInput>
                                                <BootstrapInput @bind-Value="value.MutLanguage.ja_JP" OnValueChanged="(v)=>{return FormDesigner.StateHasChangedAsync();}"></BootstrapInput>
                                            </CollapseItem>
                                        </CollapseItems>
                                    </Collapse>
                                </EditTemplate>
                            </EditorItem>

                        </FieldItems>
                    </EditorForm>
                </ValidateForm>

            </CollapseItem>

            <CollapseItem Text="Ext(扩展属性)">
                @if (ComponentData is not null)
                {
                    @foreach (var item in @ComponentData.Props)
                    {
                        @switch (item.Value.PType)
                        {
                            case PType.String:
                                <BootstrapInput OnValueChanged="OnValueChangedForString" ShowLabel="true" DisplayText="@item.Value.DisplayName" @bind-Value="@item.Value.StringValue"></BootstrapInput>
                                break;
                            case PType.SingleColor:
                                <Select ShowLabel="true" DisplayText="@item.Key" Color="Color.Primary" IsClearable="true" Items="ColorItems" @bind-Value="@item.Value.StringValue"></Select>
                                break;
                            case PType.Bool:
                                <Switch ShowLabel="true" DisplayText="@item.Value.DisplayName" @bind-Value="item.Value.BoolVal" OnValueChanged="OnValueChangedForBool"></Switch>
                                break;
                            case PType.Int:
                                break;
                            case PType.Double:
                                break;
                            case PType.DataSourse:
                                @*<Select IsVirtualize="true" @bind-Value="@(ComponentData.Props[""].Value)" OnQueryAsync="OnQueryAsync" ShowLabel="true" DisplayText="@item.Key" Color="Color.Primary" IsClearable="true"></Select>*@
                                <Select IsVirtualize="true" @bind-Value="@item.Value.StringValue" OnQueryAsync="OnQueryAsync" ShowLabel="true" DisplayText="@item.Key" Color="Color.Primary" IsClearable="true"></Select>
                                break;
                            default:
                                <BootstrapInput ShowLabel="true" DisplayText="@item.Key" @bind-Value="@item.Value.StringValue"></BootstrapInput>
                                break;
                        }
                    }
                }
            </CollapseItem>

        </CollapseItems>
    </Collapse>
</div>

@code
{
    [NotNull]
    private Modal? Modal { get; set; }

    [CascadingParameter(Name = "Root")]
    public FormDesigner? FormDesigner { get; set; }

    [Parameter]
    public ComponentDto? ComponentData { get; set; }

    [NotNull]
    private ValidateForm? ValidateForm { get; set; }
    [Inject]
    [NotNull]
    private DialogService? DialogService { get; set; }

    //[Inject]
    //[NotNull]
    //private ISqlSugarClient? db { get; set; }

    [Parameter]
    public EventCallback<ChangeEventArgs> OnLabelChanged { get; set; }
    [Parameter]
    public EventCallback<ChangeEventArgs> OnWidthChanged { get; set; }
    [Parameter]
    public EventCallback<ChangeEventArgs> OnHeightChanged { get; set; }
    [Parameter]
    public EventCallback<PInof> OnPropChanged { get; set; }
    /// <summary>
    /// Primary / Secondary / Success / Danger / Warning / Info / Dark
    /// </summary>
    List<SelectedItem> ColorItems = new List<SelectedItem>() {
                new SelectedItem("None", "None"),
                new SelectedItem("Active", "Active"),
                new SelectedItem("Primary", "Primary"),
                new SelectedItem("Warning", "Warning"),
                new SelectedItem("Info", "Info"),
                new SelectedItem("Danger", "Danger"),
                new SelectedItem("Success", "Success"),
                new SelectedItem("Dark", "Dark")
                };
    List<SelectedItem> BoolItems = new List<SelectedItem>() {
                new SelectedItem("True", "True"),
                new SelectedItem("False", "False"),

                };
    private SelectedItem? VirtualItem1 { get; set; }
    private async Task<QueryData<SelectedItem>> OnQueryAsync(VirtualizeQueryOption option)
    {
        await Task.Delay(200);
        return new QueryData<SelectedItem>
            {
                Items = new List<SelectedItem>()
                        {
                    new SelectedItem("北京", "北京"),
                    new SelectedItem("上海", "上海") { Active = true },
                },
                TotalCount = 2
            };
    }
    public List<SelectedItem> Parameters { get; set; } = new List<SelectedItem>() { new SelectedItem() { Value = "1", Text = "22" }, new SelectedItem() { Value = "2", Text = "22" } };
    protected override Task OnInitializedAsync()
    {
        Parameters = FormDesigner.FunctionPage.ContainerData.FindAllComponent().Select(x => new SelectedItem() { Text = "   " + x.Id, Value = x.Id }).ToList();
        return base.OnInitializedAsync();
    }
    private IEnumerable<SelectedItem> GetParameters()
    {
        return FormDesigner.FunctionPage.ContainerData.FindAllComponent().Select(x => new SelectedItem() { Text = "   " + x.Id, Value = x.Id });

    }
    private IEnumerable<SelectedItem> GetProList()
    {
        return new List<SelectedItem> { };
        // return db.DbMaintenance.GetProcList("LCM").Select(x => new SelectedItem() { Text = x, Value = x });
    }
    public void OnValueChanged(MouseEventArgs e)
    {

        //Color.Dark
    }
    public void OnFieldValueChanged(string key, object value)
    {//lstNames.GroupBy(n => n).Any(c => c.Count() > 1);
        if (key == "Id")
        {

            if (FormDesigner.FunctionPage.ContainerData.FindAllComponent(x => true).GroupBy(x => x.Id).Any(x => x.Count() > 0))
            {
                ValidateForm.SetError("Id", "数据库中已存在");
            }

        }
        //return Task.CompletedTask;
    }
    private Task OnValidComplexModel(EditContext context)
    {
        ValidateForm.SetError("Id", "数据库中已存在");
        return Task.CompletedTask;
    }
    /// <summary>
    /// 当设置控件的Id属性的时候
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public Task IdOnValueChanged(string v)
    {

        if (FormDesigner.FunctionPage.ContainerData.FindAllComponent(x => true).GroupBy(x => x.Id).Any(x => x.Count() > 1))
        {
            ComponentData.Id = "";
        }
        return Task.CompletedTask;
    }
    /// <summary>
    /// 当设置控件的Id属性的时候
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public async Task OnValueChangedForBool(bool v)
    {
        await FormDesigner.StateHasChangedAsync();
    }
    /// <summary>
    /// 当设置控件外观颜色的时候
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public async Task OnValueChangedForColor(Color v)
    {
        await FormDesigner.StateHasChangedAsync();
    }
    /// <summary>
    /// 当设置控件外观颜色的时候
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public async Task OnValueChangedForInt(int v)
    {
        await FormDesigner.StateHasChangedAsync();
    }
    /// <summary>
    /// 当设置控件外观颜色的时候
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public async Task OnValueChangedForString(string v)
    {
        await FormDesigner.StateHasChangedAsync();
    }
}



